package cn.cau.control;

import cn.cau.dao.BasicEnumDao;
import cn.cau.dao.CodeNumberDao;
import cn.cau.entity.*;
import cn.cau.service.AuditingWorkService;
import cn.cau.service.BasicEnumService;
import cn.cau.service.CodeNumberService;
import cn.cau.service.CodeRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CodeAuditingWorkFlowController {

    @Autowired
    CodeNumberService codeNumberService;

    @Autowired
    CodeRuleService codeRuleService;

    @Autowired
    BasicEnumService basicEnumService;

    @Autowired
    CodeNumberDao codeNumberDao;

    @Autowired
    AuditingWorkService auditingWorkService;

    @RequestMapping(value = "/workflow")    //到码段审核的路由
    public ModelAndView WorkFlowTable(@RequestParam(value = "id") int id){        //传递的id为具体编码的id//
        //首先找到具体编码规则的id

        CodeNumber codeNumber = codeNumberService.getAllRecored(id).get(0);


        int rule_id = codeNumber.getCodeRuleId();
        CodeRule rule= codeRuleService.findCodeRuleById(rule_id);
        String number = codeNumber.getCodeNumber();
        int num_of_flow = rule.getCodeCodeCount();

        List<BasicEnum> enums = new ArrayList<>();

        int cur_index = 3;    //第一个码段的下标为3

        for(int i=1;i<=num_of_flow;i++){
            String cur_enumration = number.substring(cur_index,cur_index+2)+'0';
            char state = number.charAt(cur_index+2);
            BasicEnum basicEnum = auditingWorkService.findBasicEnumByRuleIdPosEnumeration(rule_id,i,cur_enumration);
            basicEnum.setCodeLength((int)(state-'0'));
            System.out.println(basicEnum);
            System.out.println("======================================================================================");
            cur_index+=3;
            enums.add(basicEnum);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("lists",enums);
        modelAndView.addObject("code_id",id);
        modelAndView.setViewName("codeAuditing/codeAuditingWorkFlow");
        return modelAndView;
    }

    @RequestMapping(value = "/update")    //到码段审核的路由
    public String Update(@RequestParam(value = "id") int id,@RequestParam(value = "pos") int pos){
        /*
        id 为具体编码的id（pyq表中）
        pos为第几个码段
         */
        ModelAndView modelAndView=new ModelAndView();
        CodeNumber codeNumber = codeNumberService.getAllRecored(id).get(0);
        String cur_number = codeNumber.getCodeNumber();

        int modify_index = (pos)*3+2;    //要修改的位数

        StringBuffer sb = new StringBuffer(cur_number);
        sb.setCharAt(modify_index,'1');

        codeNumberDao.updateNumber(sb.toString(),id);
        modelAndView.setViewName("codeAuditing/codeAuditingIndex");
        return "redirect:/workflow?id="+id;
    }
}
